/*
 * @Author: zi.yang
 * @Date: 2022-02-08 19:48:09
 * @LastEditors: zi.yang
 * @LastEditTime: 2022-02-09 18:15:04
 * @Description: In User Settings Edit
 * @FilePath: \api-generator\app\model\api-doc.js
 */

'use strict';

const dayjs = require('dayjs');
const { utcOffset: utc } = require('../utils/global');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const ApiDocSchema = new Schema({
    docName: {
      type: String,
      unique: false,
      required: false,
      default: '',
    },
    apiDoc: {
      type: Object,
      unique: false,
      required: true,
    },
    description: {
      type: String,
      unique: false,
      required: false,
      default: '',
    },
    host: {
      type: String,
      unique: false,
      required: false,
      default: '0.0.0.0',
    },
    version: {
      type: String,
      unique: false,
      required: false,
      default: 'v0.1.0',
    },
    create_by: {
      type: String,
      unique: false,
      required: true,
    },
    createdAt: {
      type: Date,
      unique: false,
      require: true,
      get(v) {
        return dayjs(utc(v)).format('YYYY-MM-DD HH:mm:ss');
      },
      set(v) {
        return utc(v);
      },
      default() {
        return utc();
      },
    },
    updatedAt: {
      type: Date,
      unique: false,
      require: true,
      get(v) {
        return dayjs(utc(v)).format('YYYY-MM-DD HH:mm:ss');
      },
      set(v) {
        return utc(v);
      },
      default() {
        return utc();
      },
    },
    update_by: {
      type: String,
      unique: false,
      required: true,
    },
  },
  { timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } }
  );

  ApiDocSchema.set('toObject', { getters: true });
  ApiDocSchema.set('toJSON', { getters: true });
  return mongoose.model('ApiDoc', ApiDocSchema);
};
